﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;

namespace PV_Performance_Report
{
    class Reportor
    {
        public Reportor(string path)
        {
            m_path = path;
            m_result_path = m_path + @"\result";
            m_handler = new Report_handler(m_path, m_result_path);
        }

        public int Run()
        {
            if (!Directory.Exists(m_path))
            {
                Console.WriteLine("cannot find the directory for source files![" + m_path + "]");
                return -1;
            }

            if (!Directory.Exists(m_result_path))
            {
                Directory.CreateDirectory(m_result_path);
            }

            pro_agent();
            test_console_command();
            test_console_event();
            poll_table_count();
            trigger_rule();
            pvdb_perf_counter_collect();
            pvserver_perf_counter_collect();

            final_report();
            Console.WriteLine("Done! Press any key to exit...");
            return 0;
        }

        private int poll_table_count()
        {
            Console.WriteLine("Generate poll_table_count report...");
            string src_csv_file = m_path + @"\poll_table_count.csv";
            string csv_file = m_result_path + @"\poll_table_count.csv";
            string xlsx_file = m_result_path + @"\poll_table_count.xlsx";

            string[] file_list = Directory.GetFiles(m_path, "poll_table_count.csv", SearchOption.TopDirectoryOnly);
            if (file_list == null || file_list.Length == 0)
                return -1;

            if (m_handler.modify_time_csv(src_csv_file, csv_file, CommenDefine.File_Name_e.POLL_TABLE_COUNT) < 0)
                return -1;

            if (m_handler.gen_excel(csv_file, xlsx_file, CommenDefine.File_Name_e.POLL_TABLE_COUNT) < 0)
                return -1;

            //File.Delete(csv_file);
            return 0;
        }

        private int trigger_rule()
        {
            Console.WriteLine("Generate trigger_rule report...");
            string[] file_list = Directory.GetFiles(m_path, "*triggerrule*.csv", SearchOption.TopDirectoryOnly);
            if (file_list == null || file_list.Length == 0)
                return -1;

            string src_csv_file = file_list[0];
            string csv_file = m_result_path + @"\trigger_rule.csv";
            string xlsx_file = m_result_path + @"\trigger_rule.xlsx";

            if (m_handler.modify_time_csv(src_csv_file, csv_file, CommenDefine.File_Name_e.TRIGGER_RULE) < 0)
                return -1;

            if (m_handler.gen_excel(csv_file, xlsx_file, CommenDefine.File_Name_e.TRIGGER_RULE) < 0)
                return -1;

            //File.Delete(csv_file);
            return 0;
        }

        private int pvserver_perf_counter_collect()
        {
            Console.WriteLine("Generate pvserver_perf_counter_collect report...");
            string[] file_list = Directory.GetFiles(m_path, "*SERVER*perf_counter_collect.csv", SearchOption.TopDirectoryOnly);
            if (file_list == null || file_list.Length == 0)
                return -1;

            string src_csv_file = file_list[0];
            string csv_file = m_result_path + @"\PVSERVER_perf_counter_collect.csv";
            string xlsx_file = m_result_path + @"\PVSERVER_perf_counter_collect.xlsx";

            if (m_handler.modify_time_csv(src_csv_file, csv_file, CommenDefine.File_Name_e.SVR_COLLECT) < 0)
                return -1;

            if (m_handler.gen_excel(csv_file, xlsx_file, CommenDefine.File_Name_e.SVR_COLLECT) < 0)
                return -1;

            //File.Delete(csv_file);
            return 0;
        }

        private int pvdb_perf_counter_collect()
        {
            Console.WriteLine("Generate pvdb_perf_counter_collect report...");

            string[] file_list = Directory.GetFiles(m_path, "*DB*perf_counter_collect.csv", SearchOption.TopDirectoryOnly);
            if (file_list == null || file_list.Length == 0)
                return -1;

            string src_csv_file = file_list[0];
            string csv_file = m_result_path + @"\PVDB-perf_counter_collect.csv";
            string xlsx_file = m_result_path + @"\PVDB-perf_counter_collect.xlsx";

            if (m_handler.modify_time_csv(src_csv_file, csv_file, CommenDefine.File_Name_e.DB_COLLECT) < 0)
                return -1;

            if (m_handler.gen_excel(csv_file, xlsx_file, CommenDefine.File_Name_e.DB_COLLECT) < 0)
                return -1;

            //File.Delete(csv_file);
            return 0;
        }

        private int pro_agent()
        {
            Console.WriteLine("Generate ProAgent report...");
            string csv_file = m_result_path + @"\PROAGENT1.csv";
            string xlsx_file = m_result_path + @"\PROAGENT1.xlsx";

            if (m_handler.combine_files("PROAGENT*.csv", csv_file) < 0)
                return -1;

            if (m_handler.modify_time_csv(csv_file, csv_file, CommenDefine.File_Name_e.PROAGENT) < 0)
                return -1;

            if (m_handler.gen_excel(csv_file, xlsx_file, CommenDefine.File_Name_e.PROAGENT) < 0)
                return -1;

            //File.Delete(csv_file);
            return 0;
        }

        private int test_console_command()
        {
            Console.WriteLine("Generate test_console_command report...");
            string csv_file = m_result_path + @"\TESTCONSOLE1_command.csv";
            string xlsx_file = m_result_path + @"\TESTCONSOLE1_command.xlsx";

            if (m_handler.combine_files("TESTCONSOLE*_command.csv", csv_file) < 0)
                return -1;

            if (m_handler.modify_time_csv(csv_file, csv_file, CommenDefine.File_Name_e.TEST_CONSOLE_COMMAND) < 0)
                return -1;

            if (m_handler.gen_excel(csv_file, xlsx_file, CommenDefine.File_Name_e.TEST_CONSOLE_COMMAND) < 0)
                return -1;

            //File.Delete(csv_file);
            return 0;
        }

        private int test_console_event()
        {
            Console.WriteLine("Generate test_console_event report...");
            string csv_file = m_result_path + @"\TESTCONSOLE1_event.csv";
            string xlsx_file = m_result_path + @"\TESTCONSOLE1_event.xlsx";

            if (m_handler.combine_files("TESTCONSOLE*_event.csv", csv_file) < 0)
                return -1;

            if (m_handler.modify_time_csv(csv_file, csv_file, CommenDefine.File_Name_e.TEST_CONSOLE_EVENT) < 0)
                return -1;

            if (m_handler.gen_excel(csv_file, xlsx_file, CommenDefine.File_Name_e.TEST_CONSOLE_EVENT) < 0)
                return -1;

            //File.Delete(csv_file);
            return 0;
        }

        private int final_report()
        {
            Console.WriteLine("Generate final report...");
            string[] file_list = Directory.GetFiles(m_path, "TestResult_Template.docx", SearchOption.TopDirectoryOnly);
            if (file_list == null || file_list.Length == 0)
            {
                Console.WriteLine("cannot find the template docx file[TestResult_Template.docx] for final report");
                return -1;
            }

            Object path = m_path + @"\TestResult_Template.docx";
            Object resutl_path = m_result_path + @"\TestResult.docx";
            File.Copy(path.ToString(), resutl_path.ToString(), true);

            m_handler.gen_doc(resutl_path);

            file_list = Directory.GetFiles(m_result_path, "*.xlsx", SearchOption.TopDirectoryOnly);
            if (file_list != null || file_list.Length > 0)
            {
                for (int i = 0; i < file_list.Length; i++)
                {
                    //File.Delete(file_list[i]);
                }
            }
            return 0;        
        }

        private Report_handler m_handler;
        private string m_path;
        private string m_result_path;
    }
}
